package com.okoj.excel_lib_rary.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.material.tabs.TabLayout;
import com.okoj.excel_lib_rary.R;
import com.okoj.excel_lib_rary.adapter.TemplateAdapter;
import com.okoj.excel_lib_rary.data.RemoteResp;
import com.okoj.excel_lib_rary.data.entity.Kind;
import com.okoj.excel_lib_rary.data.entity.Template;
import com.okoj.excel_lib_rary.listener.LoadDataListener;
import com.okoj.excel_lib_rary.util.WpsHelper;

import java.util.ArrayList;
import java.util.List;

public class TemplateListFragment extends Fragment implements TabLayout.OnTabSelectedListener {

    TabLayout tabLayout;
    RecyclerView recyclerView;
    TemplateAdapter adapter;
    List<Kind> kinds = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.wps_fragment_template_list, container, false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tabLayout = view.findViewById(R.id.tab);
        recyclerView = view.findViewById(R.id.rv_list);
        tabLayout.addOnTabSelectedListener(this);
        initRv();
        getAllKind();
    }

    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        try {
            if (kinds.size() == 0){
                return;
            }
            String id = kinds.get(tab.getPosition()).getId().toString();
            getTemplate(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onTabUnselected(TabLayout.Tab tab) {

    }

    @Override
    public void onTabReselected(TabLayout.Tab tab) {

    }

    private void initRv(){
        GridLayoutManager gridLayoutManager = new GridLayoutManager(requireContext(),2);
        recyclerView.setLayoutManager(gridLayoutManager);
        adapter = new TemplateAdapter(R.layout.wps_item_template);
        recyclerView.setAdapter(adapter);
        adapter.setOnItemClickListener((adapter, view, position) -> {
            String templateId = this.adapter.getItem(position).getFilemodulemodel().getId().toString();
            String fileName = this.adapter.getItem(position).getFilemodulemodel().getOss_file();
            WpsHelper.openTemplate(getContext(),templateId,fileName);
        });
    }

    private void getAllKind(){
        RemoteResp.getAllKind(new LoadDataListener<List<Kind>>() {
            @Override
            public void onStart() {
            }

            @Override
            public void onSuccess(List<Kind> kindData) {
                kinds.clear();
                kinds.addAll(kindData);
                tabLayout.removeAllTabs();
                for (Kind kind:kindData){
                    TabLayout.Tab tab = tabLayout.newTab().setText(kind.getName());
                    tabLayout.addTab(tab);
                }
                getTemplate(kinds.get(0).getId().toString());
            }

            @Override
            public void onError(String msg) {
                Toast.makeText(getContext(),msg,Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void getTemplate(String kindId){

        RemoteResp.getTemplateByKind(kindId, new LoadDataListener<List<Template.SonFilesBean>>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onSuccess(List<Template.SonFilesBean> data) {
                adapter.setNewInstance(data);
            }

            @Override
            public void onError(String msg) {
                Toast.makeText(getContext(),msg,Toast.LENGTH_SHORT).show();
            }
        });

    }
}
